// pages/login/login.js
var app = getApp()
Page({
  data: {
    uError: true,
    pError: true,
    phoneError: true,
    rpError: true,
    loginView: true,
  },
  formLogin: function(data) {
    var username = data.detail.value.sms_user;
    var password = data.detail.value.sms_pwd;
    wx.request({
      url: app.globalData.smsbaoApi + "/jwt/login.action?username=" + username + "&password=" + password,
      method: 'post',
      header: {
        'content-type': 'application/json',
        'appId': app.globalData.appId,
        'appKey': app.globalData.appKey,
      },
      success: function(res) {
        var smsUserInfo = {};
        if (res.data.code == '200') {
          wx.request({
            url: app.globalData.smsbaoApi + "/jwt/member/info.action",
            method: 'get',
            header: {
              'content-type': 'application/json',
              'appId': app.globalData.appId,
              'appKey': app.globalData.appKey,
              "Authorization": res.header.token,
            },
            success: function(resInfo) {
              if (resInfo.data.code == '200') {
                smsUserInfo = {
                  "userId": resInfo.data.data.member.id,
                  "username": resInfo.data.data.member.username,
                  "token": resInfo.header.token,
                  "balance": resInfo.data.data.member.balance,
                  "leaveCount": resInfo.data.data.leaveCount,
                };
                wx.setStorageSync('smsInfo', smsUserInfo);
                wx.showToast({
                  title: '登录成功',
                  icon: 'loading',
                  duration: 2000
                })
                setTimeout(function() {
                  wx.switchTab({
                    url: '/pages/info/info'
                  })
                }, 500);
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.desc,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  formRegister: function(reData) {
    var username = reData.detail.value.sms_user;
    var password = reData.detail.value.sms_pwd;
    var rePassword = reData.detail.value.sms_repwd;
    var moblie = reData.detail.value.sms_moblie;
    var mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
    var uPattern = /^[a-zA-Z0-9_]{4,16}$/;
    if (mPattern.test(moblie) == false && moblie == '' || moblie == undefined) {
      wx.showToast({
        title: "请填写正确的手机号",
        icon: 'none',
        duration: 2000
      })
      this.setData({
        phoneError: false
      })
    }
    if (mPattern.test(username) == false && username == false || username == undefined) {
      wx.showToast({
        title: "用户名不能为空",
        icon: 'none',
        duration: 2000
      })
      this.setData({
        uError: false
      })
    }
    if (password == false || password == undefined) {
      wx.showToast({
        title: "密码格式不正确",
        icon: 'none',
        duration: 2000
      })
      this.setData({
        pError: false
      })
    }
    //注册
    wx.request({
      url: app.globalData.smsbaoApi + "/jwt/register.action?username=" + username + "&password=" + password + "&email=" + moblie + "@mobile.com " + "&qq=" + moblie + "&mobile=" + moblie,
      method: 'post',
      header: {
        'content-type': 'application/json',
        'appId': app.globalData.appId,
        'appKey': app.globalData.appKey,
      },
      success: function(res) {
        var smsUserInfo = {};
        if (res.data.code == '200') {
          wx.request({
            url: app.globalData.smsbaoApi + "/jwt/member/info.action",
            method: 'get',
            header: {
              'content-type': 'application/json',
              'appId': app.globalData.appId,
              'appKey': app.globalData.appKey,
              "Authorization": res.header.token,
            },
            success: function(sms) {
              if (sms.data.code == '200') {
                smsUserInfo = {
                  "userId": sms.data.data.member.id,
                  "username": sms.data.data.member.username,
                  "token": sms.header.token,
                  "balance": sms.data.data.member.balance,
                  "leaveCount": sms.data.data.leaveCount,
                };
                wx.setStorageSync('smsInfo', smsUserInfo);
                wx.showToast({
                  title: '注册成功',
                  icon: 'loading',
                  duration: 2000
                })
                setTimeout(function() {
                  wx.switchTab({
                    url: '/pages/info/info'
                  })
                }, 500);
              }
            },
          })
        } else {
          wx.showToast({
            title: res.data.desc,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },


  userData: function(e) {
    var uPattern = /^[a-zA-Z0-9_]{4,16}$/;
    if (uPattern.test(e.detail.value) == false && e.detail.value == '' || e.detail.value == undefined) {
      this.setData({
        uError: false
      })
    } else {
      this.setData({
        uError: true
      })
    }
  },
  pwdData: function(e) {
    var uPattern = /^[a-zA-Z0-9_]{4,16}$/;
    if (uPattern.test(e.detail.value) == false && e.detail.value == '' || e.detail.value == undefined) {
      this.setData({
        pError: false,
        pwd: e.detail.value
      })
    } else {
      this.setData({
        pError: true,
        pwd: e.detail.value
      })
    }
  },
  repwdData: function(e) {
    if (e.detail.value != this.data.pwd) {
      this.setData({
        rpError: false,
      })
    } else {
      this.setData({
        rpError: true,
      })
    }
  },
  mobileData: function(e) {
    var mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
    if (mPattern.test(e.detail.value) == false && e.detail.value == '' || e.detail.value == undefined) {
      this.setData({
        phoneError: false,
      })
    } else {
      this.setData({
        phoneError: true,
      })
    }
  },
  registerSms: function(e) {
    this.setData({
      loginView: !this.data.loginView,
    })
  }
})